﻿using System;
using System.Windows.Forms;

using LuaInterface;

namespace RwotaStatistic.LuaToMssql
{
	public partial class Form1 : Form
	{
		private Lua _luaVm;

		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			_luaVm = new Lua();
		}

		private void btnConvert_Click(object sender, EventArgs e)
		{
			_luaVm.DoFile(txtFilePath.Text);
			var table = _luaVm.GetTable(txtTableName.Text);
			if (table == null)
			{
				MessageBox.Show("Table not found.", Text);
				return;
			}
			var dt = LuaTableConvert.ToDataTable(table, (int)nudFactsLevel.Value);

			var dao = new DataTableDao(txtTableName.Text, dt, txtConnectionString.Text);
			if(cbCreateTable.Checked)
			{
				dao.CreateTable();
			}
			dao.Save();

			MessageBox.Show("Success", Text);
		}

		private void btnBrowse_Click(object sender, EventArgs e)
		{
			var dr = openFileDialog.ShowDialog(this);
			if (dr != DialogResult.OK)
			{
				return;
			}

			txtFilePath.Text = openFileDialog.FileName;
		}
	}
}